const http = require('../../utils/http.js')
Component({
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    isShow: {
      type: Boolean
    },
    outTradeNo: {
      type: String
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    send_coupon_params: [],
    sign: "",
    send_coupon_merchant: "",
  },

  observers: {
    'isShow, outTradeNo': function (isShow, outTradeNo) {//  'params'是要监听的字段，（params）是已更新变化后的数据
      console.log(isShow, outTradeNo)
      if (isShow) {
        http.get("/api/index/getMerCouponSign", {outTradeNo: this.properties.outTradeNo}).then(res=>{
          if (res.code == 200) {
            this.setData({
              send_coupon_params: res.result.send_coupon_params,
              sign: res.result.sign,
              send_coupon_merchant: res.result.send_coupon_merchant,
            })
          }
        })
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //插件发放完成之后的回调方法
    getcoupon: function (params) {
      let that = this;
      that.setData({
        isShow: false,
        outTradeNo: ''
      })
      let result = params.detail.send_coupon_result[0]
      console.log(result)
      if (result.code=='SUCCESS'){
        http.get("/api/index/sendCouponSuccess", {outTradeNo:result.out_request_no, couponCode:result.coupon_code}).then(res => {
          wx.showToast({
            title: '发券成功，请往微信卡包里查看！',
            icon: 'none',
            duration: 800
          })
        })
      }else {
        wx.showToast({
          title: result.message,
          icon: 'none',
          duration: 2000
        })
      }

    },
    hideModal() {
      this.setData({
        isShow:false
      })
    },
  }
})
